#include <iostream>
#include <cassert>
using namespace std;

double READ_CC = 0;
double WRITE_CC = 0;

struct Elem
{
	double *data;
	int idx;

	Elem(double *_data, int _idx) :
		data(_data), idx(_idx)
		{
		}

	operator double()
	{
		READ_CC++;
		return data[idx];
	}

	Elem operator=(double v)
	{
		WRITE_CC++;
		data[idx] = v;
		return *this;
	}

	Elem operator=(Elem v)
	{
		*this = double(v);
		return *this;
	}
};

struct MatrixRow
{
	double *ptr;
	MatrixRow(double *_ptr) : ptr(_ptr) {}

	Elem operator[](int i)
	{
		return Elem(ptr, i);
	}
};

// 简单的矩阵(复制的时候只复制meta，不复制实际数据)
struct Matrix
{
	int row, col, size;
	double *data;

	Matrix(int _row=1, int _col=1) : row(_row), col(_col)
	{
		size = row * col;
		data = new double[size];
		memset(data, 0, sizeof(double) * size);
	}

	inline MatrixRow operator[](int i) {
		assert(i < row);
		return MatrixRow(data + i * col);
	}
};

// 简单的向量(复制的时候只复制meta，不复制实际数据)
struct Vector
{
	int size;
	double* data;

	Vector(int _size=1) : size(_size)
	{
		data = new double[size];
		memset(data, 0, sizeof(double) * size);
	}

	Elem operator[](int x)
	{
		return Elem(data, x);
	}
};

void show()
{
	cout << "write_cc=" << WRITE_CC << " read_cc=" << READ_CC << endl;
	WRITE_CC = READ_CC = 0;
}

int main()
{
	Vector v(10);
	v[0] = 1;
	show();
	v[0] = v[0] + 1;
	show();
	v[0] = v[1] + 1;
	show();
	v[0] = v[1];
	show();
	Matrix m(10,10);
	m[0][0] = m[2][2] + m[3][3];
	show();
	return 0;
}

